package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.CompanyBalanceAddDTO;
import org.jsola.hr.dto.CompanyBalanceUpdateDTO;
import org.jsola.hr.query.CompanyBalanceQuery;
import org.jsola.hr.service.ICompanyBalanceService;
import org.jsola.hr.vo.CompanyBalanceListVO;
import org.jsola.hr.vo.CompanyBalanceVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 用工单位账面余额表
 *
 * @author zhr
 */
@RestController("hrCompanyBalanceController")
@RequestMapping(HrConstants.API_PATH + "/v1/companyBalances")
public class CompanyBalanceController implements ICompanyBalanceControllerAPI {

    @Autowired
    private ICompanyBalanceService companyBalanceService;

    @Override
    @PostMapping
    public Result<CompanyBalanceVO> save(@Valid @RequestBody CompanyBalanceAddDTO companyBalanceAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增用工单位账面余额表权限校验

        return Result.success(
                companyBalanceService.save(companyBalanceAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody CompanyBalanceUpdateDTO companyBalanceUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改用工单位账面余额表权限校验

        int count = companyBalanceService.update(companyBalanceUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{companyBalanceId}")
    public Result<Integer> delete(@PathVariable Long companyBalanceId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除用工单位账面余额表权限校验

        int count = companyBalanceService.deleteByIds(tokenUser, companyBalanceId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] companyBalanceIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除用工单位账面余额表权限校验

        int count = companyBalanceService.deleteByIds(tokenUser, companyBalanceIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{companyBalanceId}")
    public Result<CompanyBalanceVO> findById(@PathVariable Long companyBalanceId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看用工单位账面余额表权限校验

        return Result.success(
                companyBalanceService.selectById(companyBalanceId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<CompanyBalanceListVO>> listAll(@Valid CompanyBalanceQuery companyBalanceQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看用工单位账面余额表权限校验

        return Result.success(
                companyBalanceService.select(companyBalanceQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<CompanyBalanceListVO>> page(@Valid CompanyBalanceQuery companyBalanceQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                companyBalanceService.selectPage(companyBalanceQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping("/selectBySiteId")
    public Result<CompanyBalanceVO> selectBySiteId() {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                companyBalanceService.selectBySiteId(tokenUser.getSiteId())
        );
    }

    @Override
    @PostMapping("/selectByCompanyIds")
    public Result<CompanyBalanceVO> selectByCompanyIds(@RequestBody String[] companyIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                companyBalanceService.selectByCompanyIds(companyIds, tokenUser.getSiteId())
        );
    }


}

